package cn.com.css.taxInspection.workflow.controller;

import cn.com.css.taxInspection.workflow.entity.*;
import cn.com.css.taxInspection.workflow.service.*;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/workflow/log/survey")
public class WorkflowLogSurveyController {
    @Resource
    private WorkflowLogSurveyService surveyService;
    @Resource
    private WorkflowLogSurveyContentService surveyContentService;
    @Resource
    private GzddmbService gzddmbService;
    @Resource
    private GzddmbContentService gzddmbContentService;
    @Resource
    private WorkflowLogSurveyMessageService surveyMessageService;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private WorkflowLogSurveyFeedbackService surveyFeedbackService;
//    @Value("${file.path}")
//    private String filePath;
//    @Value("${file.url}")
//    private String fileUrl;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @ResponseBody
    public DataResult add(@RequestBody WorkflowLogSurveyContentEntity surveyContentEntity) {
        String id = surveyContentEntity.getId();
        // 如果入参存在ID，说明是一个更新，先清理数据
        if (TsBqUtils.isNotNull(surveyContentEntity.getId())) {
            List<String> ids = new ArrayList<>();
            ids.add(id);
            this.del(ids);
        }
        return surveyService.saveSurveyAndContent(surveyContentEntity);
    }

    @ApiOperation(value = "保存工作调度模板")
    @PostMapping("/saveGzddmb")
    @ResponseBody
    @DS("oracle")
    public DataResult saveGzddmb(@RequestBody GzddmbContentEntity gzddmbContentEntity) {
        String id = gzddmbContentEntity.getId();
        // 如果入参存在ID，说明是一个更新，先清理数据
        if (TsBqUtils.isNotNull(gzddmbContentEntity.getId())) {
            List<String> ids = new ArrayList<>();
            ids.add(id);
            this.del(ids);
        }
        return gzddmbService.saveGzddmb(gzddmbContentEntity);
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/del")
    @ResponseBody
    public DataResult del(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        for (String _id : ids) {
            surveyService.removeById(_id);
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("SURVEY_ID", _id);
            surveyContentService.removeByMap(columnMap);
        }
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/removeGzddmb")
    @ResponseBody
    @DS("oracle")
    public DataResult removeGzddmb(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        for (String _id : ids) {
            gzddmbService.deleteDataById(_id);
            gzddmbContentService.deleteDataById(_id);
        }
        return DataResult.success();
    }

    @ApiOperation(value = "发布")
    @DeleteMapping("/push")
    @ResponseBody
    @DS("oracle")
    public DataResult push(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        for (String _id : ids) {
            WorkflowLogSurveyEntity workflowLogSurvey = surveyService.getById(_id);
            workflowLogSurvey.setState("1");
            surveyService.updateById(workflowLogSurvey);
        }
        return DataResult.success();
    }

    @ApiOperation(value = "发布工作调度模板")
    @DeleteMapping("/pushGzddmb")
    @ResponseBody
    @DS("oracle")
    public DataResult pushGzddmb(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        for (String _id : ids) {
            GzddmbEntity gzddmbEntity = gzddmbService.getById(_id);
            gzddmbEntity.setState("1");
            gzddmbService.updateById(gzddmbEntity);
        }
        return DataResult.success();
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "列表")
    @PostMapping("/list")
    @ResponseBody
    @DS("oracle")
    public DataResult list(@RequestBody WorkflowLogSurveyEntity survey) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(survey.getPage(), survey.getLimit());
        LambdaQueryWrapper<WorkflowLogSurveyEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowLogSurveyEntity::getCreateId, currentUserId);
        if (TsBqUtils.isNotNull(survey.getTitle())) {
            queryWrapper.eq(WorkflowLogSurveyEntity::getTitle, survey.getTitle());
        }
        IPage<WorkflowLogSurveyEntity> iPage = surveyService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "查询工作调度模板")
    @PostMapping("/queryGzddmb")
    @ResponseBody
    @DS("oracle")
    public DataResult queryGzddmb(@RequestBody GzddmbEntity gzddmbEntity) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(gzddmbEntity.getPage(), gzddmbEntity.getLimit());
        LambdaQueryWrapper<GzddmbEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        if (TsBqUtils.isNotNull(gzddmbEntity.getTitle())) {
            queryWrapper.like(GzddmbEntity::getTitle, gzddmbEntity.getTitle());
        }
        queryWrapper.orderByDesc(GzddmbEntity::getCreateTime);
        IPage<WorkflowLogSurveyEntity> iPage = gzddmbService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "列表")
    @PostMapping("/group")
    @RequiresPermissions("tssq:survey:group")
    @ResponseBody
    public DataResult group(@RequestBody WorkflowLogSurveyMessageEntity surveyMessage) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(surveyMessage.getPage(), surveyMessage.getLimit());
        QueryWrapper<WorkflowLogSurveyMessageEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<WorkflowLogSurveyMessageEntity> lambdaQueryWrapper = queryWrapper
                .select("survey_id,survey_title, create_time, batch,count(1) as count").lambda()
                .groupBy((WorkflowLogSurveyMessageEntity::getCreateTime), (WorkflowLogSurveyMessageEntity::getSurveyId),
                        (WorkflowLogSurveyMessageEntity::getSurveyTitle), (WorkflowLogSurveyMessageEntity::getBatch));
        lambdaQueryWrapper.eq(WorkflowLogSurveyMessageEntity::getCreateId, currentUserId);
        if (TsBqUtils.isNotNull(surveyMessage.getSurveyTitle())) {
            lambdaQueryWrapper.like(WorkflowLogSurveyMessageEntity::getSurveyTitle, surveyMessage.getSurveyTitle());
        }
        IPage<WorkflowLogSurveyMessageEntity> iPage = surveyMessageService.page(page, lambdaQueryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "列表详情")
    @PostMapping("/feedBack")
    @ResponseBody
    public DataResult feedBack(@RequestBody WorkflowLogSurveyMessageEntity surveyMessage) {
        String djxh = surveyMessage.getDjxh();
        String surveyId = surveyMessage.getSurveyId();
        List<Object> list = new ArrayList<>();
        LambdaQueryWrapper<WorkflowLogSurveyFeedbackEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogSurveyFeedbackEntity::getSurveyId, surveyId);
        queryWrapper.eq(WorkflowLogSurveyFeedbackEntity::getDjxh, djxh);
        List<WorkflowLogSurveyFeedbackEntity> surveyFeedbackList = surveyFeedbackService.list(queryWrapper);
        LambdaQueryWrapper<WorkflowLogSurveyContentEntity> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(WorkflowLogSurveyContentEntity::getSurveyId, surveyId);
        List<WorkflowLogSurveyContentEntity> surveyContentList = surveyContentService.list(queryWrapper1);
        list.add(surveyFeedbackList);
        list.add(surveyContentList);
        return DataResult.success(list);
    }

    @ApiOperation(value = "列表详情")
    @PostMapping("/select")
    @ResponseBody
    @DS("oracle")
    public DataResult select(@RequestBody WorkflowLogSurveyEntity surveyMessage) {
        log.debug("WorkflowLogSurveyEntity:{}", surveyMessage);
        LambdaQueryWrapper<WorkflowLogSurveyEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogSurveyEntity::getState, "1");
        queryWrapper.orderByDesc(WorkflowLogSurveyEntity::getTitle);
        List<WorkflowLogSurveyEntity> list = surveyService.list(queryWrapper);
        return DataResult.success(list);
    }

    @ApiOperation(value = "查询工作调度下拉选")
    @PostMapping("/queryAjddSelect")
    @ResponseBody
    @DS("oracle")
    public DataResult queryAjddSelect() {
        LambdaQueryWrapper<GzddmbEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddmbEntity::getState, "1");
        queryWrapper.orderByDesc(GzddmbEntity::getCreateTime);
        List<GzddmbEntity> list = gzddmbService.list(queryWrapper);
        return DataResult.success(list);
    }

    @ApiOperation(value = "通过ID获取")
    @PostMapping("/getSurveyById")
    @ResponseBody
    @DS("oracle")
    public DataResult getSurveyById(@RequestBody WorkflowLogSurveyEntity survey) {
        String id = survey.getId();
        LambdaQueryWrapper<WorkflowLogSurveyContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogSurveyContentEntity::getSurveyId, id);
        queryWrapper.eq(WorkflowLogSurveyContentEntity::getDeleted, 1);
        queryWrapper.orderByAsc(WorkflowLogSurveyContentEntity::getOptionsNo);
        List<WorkflowLogSurveyContentEntity> list = surveyContentService.list(queryWrapper);
        return DataResult.success(list);
    }

    @ApiOperation(value = "通过ID获取工作调度下拉选")
    @PostMapping("/getGzddContentById")
    @ResponseBody
    @DS("oracle")
    public DataResult getGzddContentById(@RequestBody GzddmbContentEntity gzddmbContentEntity) {
        LambdaQueryWrapper<GzddmbContentEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GzddmbContentEntity::getSurveyId, gzddmbContentEntity.getId());
        queryWrapper.eq(GzddmbContentEntity::getDeleted, 1);
        queryWrapper.orderByAsc(GzddmbContentEntity::getOptionsNo);
        List<GzddmbContentEntity> list = gzddmbContentService.list(queryWrapper);
        return DataResult.success(list);
    }
}
